// Device/pages/History/components/bpgauge-component/index.js
import {
    fetchPressureList,
    fetchUpdatePressure
} from '@/services/device';
import { customNavigate } from '@/utils/route'
import {
    showToast,
    logger
} from '@/utils/util'
const app = getApp();
import dayjs from 'dayjs';
Component({
    properties: {
        data: {
            type: Object,
            default: null
        }
    },
    observers: {
        data(value) {
            logger.log(value)
            this.setData({
                ...value
            }, this.queryList)
        }
    },
    data: {
        popup_show: false,
        start_time: null,
        end_time: null,
        list: [],
        iphoneBarHeight: app.globalData.IPHONE_BAR_HEIGHT,
        staticPrefix: app.globalData.STATIC_PREFIX,
        popup_show: false,
        user_tag: '本人',
        abnormal: '全部',
        itemTitle: '筛选',
        abnormal_options: [{
                text: '全部',
                value: '全部'
            },
            {
                text: '异常',
                value: '异常'
            }
        ],
        user_options: [{
                text: '本人',
                value: '本人'
            },
            {
                text: '配偶',
                value: '配偶'
            },
            {
                text: '子女',
                value: '子女'
            },
            {
                text: '其他',
                value: '其他'
            },
            {
                text: '未认领',
                value: '未认领'
            }
        ],
        minDate: dayjs().subtract(2, 'month').valueOf(),
        maxDate: new Date().getTime(),
        columns: [{
                text: '本人',
                value: '本人'
            },
            {
                text: '配偶',
                value: '配偶'
            },
            {
                text: '子女',
                value: '子女'
            },
            {
                text: '其他',
                value: '其他'
            }
        ]
    },
    methods: {
        async queryList() {
            const {
                start_time,
                end_time,
                user_tag,
                abnormal,
                device_imei
            } = this.data;
            const params = {
                start_time,
                end_time,
                user_tag,
                abnormal,
                device_imei
            }
            const res = await fetchPressureList(params)
            logger.log(res)
            this.setData({
                list: res.data
            })
        },
        onChange(e) {
            this.setData({
                [`${e.currentTarget.dataset.target}`]: e.detail,
                list: []
            }, this.queryList)
        },
        onConfirm() {
            const component = this.selectComponent('.calendar');
            if(component.data.currentDate.length < 2 || !component.data.currentDate[1]) {
                showToast('请选择正确的时间范围！');
                return;
            }
            this.setData({
                start_time: dayjs(this.selectComponent('.calendar').data.currentDate[0]).format('YYYY-MM-DD 00:00:00'),
                end_time: dayjs(this.selectComponent('.calendar').data.currentDate[1]).format('YYYY-MM-DD 23:59:59')
            }, () => {
                this.selectComponent('#item').toggle();
                this.queryList()
            })
        },
        onClose() {
            this.setData({
                start_time: undefined,
                end_time: undefined
            }, () => {
                this.selectComponent('#item').toggle();
                this.queryList()
            })
        },
        onShowPopup(e) {
            this.setData({
                popup_show: true,
                columns: ['本人', '配偶', '子女', '其他'],
                record_id: e.currentTarget.dataset.id
            })
        },
        async onPopupConfirm(e) {
            let params = {
                    user_tag: e.detail.value,
                    measurement_type: undefined,
                    id: this.data.record_id
                },
                res = await fetchUpdatePressure(params);;
            if (res.return_code === 'SUCCESS') {
                showToast(`认领成功`, () => {
                    this.setData({
                        popup_show: false,
                    }, this.queryList)
                })
            }
        },
        onPopupCancel() {
            this.setData({
                popup_show: false
            })
        },
        onNavigate(e) {
            logger.log(e.currentTarget.dataset.item)
            const {
                item: {
                    dbp,
                    sbp,
                    pulse,
                    measuring_time,
                    device_imei,
                    user_tag
                },
                index
            } = e.currentTarget.dataset;
            if(!user_tag) {
                showToast('认领后查看详情');
                return;
            };
            const params = {
                dbp,
                sbp,
                pulse,
                measuring_time,
                device_imei,
                user_tag,
                index
            };
            customNavigate({
                url: `/Device/pages/HistoryInfo/index`,
                params
            })
        }
    }
})